package com.xyzq.hs;

import java.math.BigDecimal;

import com.xyzq.hs.util.ByteUtils;
import com.xyzq.hs.util.Logger;
import com.xyzq.hs.util.LoggerFactory;

public class ResultSet {
	Logger log=LoggerFactory.getLogger(ResultSet.class);
	private short row=-1;
	private short rowCount=0;
	private short columnCount=0;
	private byte[][][] dataSet;
	private ResultSetMetaData meta;
	private short responseCode;
	private String errorMessage;
	
	public ResultSet(short responseCode, String errorMessage) {
		super();
		this.responseCode = responseCode;
		this.errorMessage = errorMessage;
	}

	public ResultSet(ResultSetMetaData meta,byte[][][] dataSet,short rowCount) {
		super();
		this.rowCount = rowCount;
		this.columnCount = meta.getColumnCount();
		this.dataSet = dataSet;
		this.meta=meta;
	}
	
	public boolean next()  {
		if(row<rowCount-1){
			row+=1;
			return true;
		}else{
			return false;
		}
	}
	
	public void reset()  {
		row=-1;
	}

	public String getString(int columnIndex)  {
		byte[] bytes = getBytes(columnIndex);
		char columnType = meta.getColumnType(columnIndex);
		String value=null;
		switch(columnType){
			case 'I':
				value=Integer.valueOf(ByteUtils.byte2int(bytes)).toString();
				break;
			case 'F':
				value=Double.valueOf(ByteUtils.byte2double(bytes)).toString();
				break;
			case 'C':
				value=String.valueOf(ByteUtils.byte2char(bytes));
				break;
			case 'S':
				value=ByteUtils.byte2str(bytes);
				break;
		}
		log.debug("columnName: {},columnType: {},columnValue: {}",meta.getColumnName(columnIndex),columnType,value);
		return value;
	}

	public short getShort(int columnIndex)  {
		return Short.parseShort(getString(columnIndex));
	}

	public Integer getInt(int columnIndex)  {
		return Integer.parseInt(getString(columnIndex));
	}

	public long getLong(int columnIndex)  {
		return Long.parseLong(getString(columnIndex));
	}

	public Float getFloat(int columnIndex)  {
		return Float.parseFloat(getString(columnIndex));
	}

	public Double getDouble(int columnIndex)  {
		return Double.parseDouble(getString(columnIndex));
	}
	
	public BigDecimal getBigDecimal(int columnIndex)  {
        return new BigDecimal(getString(columnIndex));
	}

	public byte[] getBytes(int columnIndex)  {
		if(columnIndex>columnCount)
			throw new IllegalArgumentException("columnIndex out of index.");
		else 
			return dataSet[row][columnIndex];
	}
	
	public String getString(String columnName)  {
		return getString(meta.getColumnIndex(columnName));
	}
	
	public short getShort(String columnName)  {
		return Short.parseShort(getString(columnName));
	}
	
	public Integer getInt(String columnName)  {
		return Integer.parseInt(getString(columnName));
	}
	
	public Long getLong(String columnName)  {
		return Long.parseLong(getString(columnName));
	}
	
	public Float getFloat(String columnName)  {
		return Float.parseFloat(getString(columnName));
	}
	
	public Double getDouble(String columnName)  {
		return Double.parseDouble(getString(columnName));
	}
	
	public byte[] getBytes(String columnName)  {
		int columnIndex=meta.getColumnIndex(columnName);
		return getBytes(columnIndex);
	}
	
	public BigDecimal getBigDecimal(String columnName)  {
		 return new BigDecimal(getString(columnName));

	}

	public ResultSetMetaData getMetaData()  {
		return meta;
	}

	public Short getRow()  {
		return row;
	}
	
	public Short getRowCount()  {
		return rowCount;
	}
	
	

	public short getResponseCode() {
		return responseCode;
	}

	public void setResponseCode(short responseCode) {
		this.responseCode = responseCode;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	@Override
	public String toString() {
		StringBuffer sb=new StringBuffer();
		sb.append("Total row:"+rowCount+",column:"+columnCount+"\n");
		sb.append("\n=============================================\n");
		for(int i=0;i<columnCount;i++ ){
			sb.append(meta.getColumnName(i)).append("    ");
		}
		sb.append("\n=============================================\n");
		for(int row=0;row<rowCount;row++){
			for(int col=0;col<columnCount;col++ ){
				char columnType = meta.getColumnType(col);
				switch(columnType){
					case 'I':
						sb.append(ByteUtils.byte2int(dataSet[row][col])).append("    ");
						break;
					case 'F':
						sb.append(ByteUtils.byte2float(dataSet[row][col])).append("    ");
						break;
					case 'C':
						sb.append(ByteUtils.byte2char(dataSet[row][col])).append("    ");
						break;
					case 'S':
						sb.append(ByteUtils.byte2str(dataSet[row][col])).append("    ");
						break;
				}
			}
			sb.append("\n");
		}
		return sb.toString();
	}
	
	
}
